package cn.stard.org.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.util.PageList;
import cn.stard.basic.util.AjaxResult;
import cn.stard.org.domain.SystemDetail;
import cn.stard.org.query.SystemDetailQuery;
import cn.stard.org.service.ISystemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*复合注解拥有两个的所有功能*/
@RestController
    @RequestMapping("/systemd")
public class SystemDController {
    @Autowired
    private ISystemDetailService systemtTypeService;


    @PostMapping
    @PreAuthorize(name="查询所有数据字典分页", sn = "systemD:addorupt")
    public PageList<SystemDetail> list(@RequestBody SystemDetailQuery systemDetailQuery, Long typeid){

        System.out.println("进入查询");
        System.out.println(systemDetailQuery);
        PageList<SystemDetail> data = systemtTypeService.queryData(systemDetailQuery);

        return data;
    }


    @GetMapping("/{id}")
    @PreAuthorize(name="查询数据字典根据id", sn = "systemD:loadAll1")
    public List<SystemDetail> loadAll1(@PathVariable("id")Long id){
        List<SystemDetail> systemDetails = systemtTypeService.loadAll1(id);
        return systemDetails;

    }
    @GetMapping()
    @PreAuthorize(name="查询数据字典根据所有数据", sn = "systemD:loadAll1")
    public List<SystemDetail> loadAll1(){
        List<SystemDetail> systemDetails = systemtTypeService.loadAll();
        return systemDetails;

    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="根据id删除数据字典", sn = "systemD:dele1")
    public AjaxResult dele1(@PathVariable("id")Long id){
        try {
            System.out.println("执行1");
            systemtTypeService.remove(id);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }

    @PatchMapping
    @PreAuthorize(name="批量删除数据字典", sn = "systemD:deles")
    public AjaxResult deles(@RequestBody List<Long> ids){
        try {

            systemtTypeService.deles(ids);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }


    @PutMapping
    @PreAuthorize(name="数据字典添加修改", sn = "systemD:addorupt")
    public AjaxResult addorupt(@RequestBody  SystemDetail systemDetail){
        try {
            if (systemDetail.getId()==null){
                System.out.println(systemDetail);
                systemtTypeService.save(systemDetail);

            }else {
                systemtTypeService.update(systemDetail);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }

}
